<template>
  <div class="app-container">
    <!-- 把所有字段都生成了，先删除不要的字段，再做定制化 -->
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true" label-width="120px">
      <el-form-item label="文件原始名" prop="fileOriginalNameLike">
        <el-input v-model="queryParams.fileOriginalNameLike" placeholder="请输入文件原始名" clearable size="small" style="width: 240px" />
      </el-form-item>
      <el-form-item label="文件扩展名" prop="fileExtNameLike">
        <el-input v-model="queryParams.fileExtNameLike" placeholder="请输入文件扩展名" clearable size="small" style="width: 240px" />
      </el-form-item>
      <el-form-item label="ZFS类型" prop="zfsType">
        <el-select v-model="queryParams.zfsType" clearable placeholder="请选择ZFS类型" size="small" style="width: 240px">
          <el-option v-for="dict in zfsTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <br />
      <el-form-item label="文件MIME类型" prop="contentTypeLike">
        <el-input v-model="queryParams.contentTypeLike" placeholder="请输入文件MIME类型" clearable size="small" style="width: 240px" />
      </el-form-item>
      <el-form-item>
        <search-button @click="handleQuery" :perm="['uni:zfs:file:search']" />
        <reset-button @click="resetQuery" :perm="['uni:zfs:file:search']" />
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <delete-button @click="handleDelete" :disabled="multiple" :perm="['uni:zfs:file:delete']" :roleType="['app']" />
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :perm="['uni:zfs:file:search']" />
    </el-row>

    <!-- 把所有字段都生成了，先删除不要的字段，再做定制化 -->
    <el-table v-loading="loading" :data="dataList" stripe @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="文件ID" prop="fileId" align="center" :show-overflow-tooltip="true" min-width="280" />
      <el-table-column label="文件名" prop="fileName" align="center" :show-overflow-tooltip="true" min-width="280" />
      <el-table-column label="文件原始名" prop="fileOriginalName" align="center" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="文件扩展名" prop="fileExtName" align="center" :show-overflow-tooltip="true" min-width="100" />
      <el-table-column label="文件存储相对路径" prop="fileRelativePath" :show-overflow-tooltip="true" min-width="140" />
      <el-table-column label="ZFS类型" prop="zfsType" align="center" :show-overflow-tooltip="true" min-width="100" />
      <el-table-column label="文件MIME类型" prop="contentType" align="center" :show-overflow-tooltip="true" min-width="500" />
      <el-table-column label="创建者" prop="createBy" align="center" width="120" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="160" />
      <el-table-column label="更新者" prop="updateBy" align="center" width="120" />
      <el-table-column label="更新时间" prop="updateTime" align="center" width="160" />
      <el-table-column label="备注" prop="remark" :show-overflow-tooltip="true" min-width="200" />
      <el-table-column label="操作" align="center" width="240" class-name="small-padding fixed-width" fixed="right" v-if="hasRoleType(['app'])">
        <template #default="scope">
          <text-detail-button text="在线预览" @click="handleViewFile(scope.row)" :perm="['uni:zfs:file:search']" :roleType="['app']" />
          <text-detail-button @click="handleDetail(scope.row)" :perm="['uni:zfs:file:search']" :roleType="['app']" />
          <text-delete-button @click="handleDelete(scope.row)" :perm="['uni:zfs:file:delete']" :roleType="['app']" :title="'是否确认删除记录' + scope.row.fileId + '?'" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 详情对话框 -->
    <el-dialog v-el-drag-dialog :title="editForm.title" :close-on-click-modal="false" v-model="editForm.open" width="800px" append-to-body>
      <el-form ref="formRef" :model="editForm.data" label-width="160px">
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="文件ID" prop="fileId">
              <el-input v-model="editForm.data.fileId" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="文件名" prop="fileName">
              <el-input v-model="editForm.data.fileName" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="文件大小" prop="fileSize">
              <el-input v-model="editForm.data.fileSize" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="文件原始名" prop="fileOriginalName">
              <el-input v-model="editForm.data.fileOriginalName" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="文件扩展名" prop="fileExtName">
              <el-input v-model="editForm.data.fileExtName" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="文件存储相对路径" prop="fileRelativePath">
              <el-input v-model="editForm.data.fileRelativePath" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="ZFS类型" prop="zfsType">
              <el-input v-model="editForm.data.zfsType" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="文件MIME类型" prop="contentType">
              <el-input v-model="editForm.data.contentType" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="文件上传后返回存储ID" prop="storageId">
              <el-input v-model="editForm.data.storageId" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="业务Key" prop="businessKey">
              <el-input v-model="editForm.data.businessKey" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="editForm.data.remark" type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <dialog-footer :showOk="false" @cancel="cancel" />
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { search, deleteByIds, selectById, deleteBatchFile } from '@uni/api/uni/zfs/file';
import { debounceWrapper } from '@/utils/zmids';
import TextDetailButton from '@/components/Zmids/Buttons/TextDetailButton.vue';

export default {
  // 此处的名字需要与菜单中Path一致，否则多Tab页切换时会刷新
  name: 'UniZfsFile',
  components: { TextDetailButton },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 文件原始名
        fileOriginalNameLike: '',
        // 文件扩展名
        fileExtNameLike: '',
        // ZFS类型
        zfsType: '',
        // 文件MIME类型
        contentTypeLike: ''
      },
      editForm: {
        // 控制 dialog
        open: false,
        // 弹出层标题
        title: '',
        // 是否修改操作
        isModify: false,
        // 表单参数
        data: {}
      },
      zfsTypeOptions: []
    };
  },
  created() {
    this.getDicts('zfs_type').then((res) => {
      this.zfsTypeOptions = res.data;
    });
    // 查询列表
    this.getList();
  },
  methods: {
    // 查询列表
    getList: debounceWrapper(function () {
      this.loading = true;
      search(this.queryParams).then((response) => {
        this.dataList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    }),
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.editForm.isModify = false;
      this.editForm.data = {
        // 文件ID
        fileId: '',
        // 文件名
        fileName: '',
        // 文件大小
        fileSize: undefined,
        // 文件原始名
        fileOriginalName: '',
        // 文件扩展名
        fileExtName: '',
        // 文件存储相对路径
        fileRelativePath: '',
        // ZFS类型
        zfsType: '',
        // 文件MIME类型
        contentType: '',
        // 文件上传后返回存储ID
        storageId: '',
        // 业务Key
        businessKey: '',
        // 创建者
        createBy: '',
        // 创建时间
        createTime: undefined,
        // 更新者
        updateBy: '',
        // 更新时间
        updateTime: undefined,
        // 备注
        remark: ''
      };
      // 重置字段校验
      this.$refs.formRef && this.$refs.formRef.clearValidate();
      this.resetForm('formRef');
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      // 所有选中的ID
      this.ids = selection.map((item) => item.fileId);
      // 激活多选删除
      this.multiple = !selection.length;
    },
    // 新增按钮操作
    handleAdd() {
      this.reset();
      this.editForm.open = true;
      this.editForm.isModify = false;
      this.editForm.title = '新增ZFS文件信息';
    },
    // 删除按钮操作
    handleDelete: debounceWrapper(function (row) {
      const deleteIds = row ? row.fileId : this.ids.join(',');
      deleteBatchFile(deleteIds)
        .then(() => {
          this.getList();
          this.msgSuccess('删除成功');
        })
        .catch(function () {});
    }),
    // 预览操作
    handleViewFile: debounceWrapper(function(row) {
      window.open(import.meta.env.VITE_APP_BASE_API + "/uni/zfs/file/view/" + row.storageId);
    }),
    // 详情按钮操作
    handleDetail: debounceWrapper(function (row) {
      this.reset();
      selectById(row.fileId).then((response) => {
        this.editForm.data = response.data;
        this.editForm.open = true;
        this.editForm.isModify = true;
        this.editForm.title = '查看ZFS文件信息';
      });
    }),
    // 取消按钮
    cancel() {
      this.editForm.open = false;
      this.reset();
    }
  }
};
</script>

<style lang="scss" scoped></style>